package zero.tech.games.phz.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.games.logic.data.res.ResGameBase;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.phz.data.PhzConfig;
import zero.tech.games.phz.data.PhzGameBase;
import zero.tech.games.phz.data.PhzResGameBase;
import zero.tech.games.phz.data.PhzResReconnect;
import zero.tech.games.phz.data.PhzRoomCache;
import zero.tech.games.phz.data.consts.OpType;
import zero.tech.games.phz.data.consts.ZhanjiType;
import zero.tech.games.phz.data.entity.Card;
import zero.tech.games.phz.data.entity.Data;
import zero.tech.games.phz.data.entity.OpData;
import zero.tech.games.phz.data.entity.PlayerData;
import zero.tech.games.phz.data.entity.data.TipaiData;
import zero.tech.games.phz.data.entity.op.CphOpData;
import zero.tech.games.phz.data.notify.NotifyBaipai;
import zero.tech.games.phz.data.notify.NotifyChangePlayer;
import zero.tech.games.phz.data.notify.NotifyCphOp;
import zero.tech.games.phz.data.notify.NotifyGameStart;
import zero.tech.games.phz.data.notify.NotifyOutCard;
import zero.tech.games.phz.data.notify.NotifyQihu;
import zero.tech.games.phz.data.notify.NotifyYadi;
import zero.tech.games.phz.data.playback.OperateData;
import zero.tech.games.phz.data.req.ReqOp;
import zero.tech.games.phz.data.res.ResOutCard;
import zero.tech.games.phz.data.res.ResSetcardsLeft;
import zero.tech.wsnet.controller.LogicController;

public class PackageUtil {

	public static NotifyGameStart packageNotifyGameStart(Room room, LogicController logicController) {
		PhzRoomCache phzRoomCache = (PhzRoomCache) room.getRoomCache();
		PhzGameBase phzGameBase = (PhzGameBase) room.getGameBase();
		NotifyGameStart notifyGameStart = new NotifyGameStart();
		PlayerData playerData = phzGameBase.getPlayers().get(logicController.getId());
		notifyGameStart.getHands().addAll(playerData.getHands());
		notifyGameStart.setBanker_last_card(phzGameBase.getBanker_last_card());
		notifyGameStart.getBirds().putAll(phzRoomCache.getBirds());
		notifyGameStart.setDipai_left(phzGameBase.getDipai().size());
		notifyGameStart.setBanker_uid(phzGameBase.getBanker_uid());
		return notifyGameStart;
	}
	
	public static NotifyBaipai packageNotifyBaipai(Room room, int uid, Data data) {
		List<Data> datas =new ArrayList<>();
		datas.add(data);
		return packageNotifyBaipai(room, uid, datas);
	}

	public static NotifyBaipai packageNotifyBaipai(Room room, int uid,List<? extends Data> datas) {
		NotifyBaipai notifyBaipai = new NotifyBaipai();
		notifyBaipai.setUid(uid);
		notifyBaipai.getBaipais().addAll(datas);
		return notifyBaipai;
	}

	public static NotifyYadi packageNotifyYadi(Room room, int uid, Card card) {
		NotifyYadi notifyYadi = new NotifyYadi();
		notifyYadi.setUid(uid);
		notifyYadi.setCard(card);
		return notifyYadi;
	}

	public static NotifyChangePlayer packageNotifyChangePlayer(Room room, int changePlayerType,Card mopai) {
		NotifyChangePlayer notifyChangePlayer = new NotifyChangePlayer();
		PhzGameBase phzGameBase = (PhzGameBase) room.getGameBase();
		notifyChangePlayer.setUid(phzGameBase.getDispatch_uid());
		notifyChangePlayer.setType(changePlayerType);
		PhzConfig phzConfig = (PhzConfig) room.getGameConfig();
		if(OpType.OUTCARD==changePlayerType) {
			notifyChangePlayer.setLeft_timer(phzConfig.getOut_card_timer());
		}
		if(OpType.MOPAI==changePlayerType) {
			notifyChangePlayer.setMopai(mopai);
		}
		return notifyChangePlayer;
	}

	public static ResReconnet packageResPdkReconnect(Room room, LogicController online) {
		ResReconnet resReconnet=new PhzResReconnect();
		PckUtil.packageResReconnect(room,online,resReconnet);
		int room_status=room.getRoom_status();
		if(room_status==RoomStatus.GAMING) {
			resReconnet.setRes_gamebase(packageResPdkGameBase(room,online));
		}
		return resReconnet;
	}

	private static ResGameBase packageResPdkGameBase(Room room, LogicController online) {
		int uid=online.getId();
		PhzRoomCache phzRoomCache = (PhzRoomCache) room.getRoomCache();
		PhzGameBase phzGameBase = (PhzGameBase) room.getGameBase();
		Map<Integer,PlayerData> playerDataMap = phzGameBase.getPlayers();
		Map<Integer, Integer> scoremap=phzRoomCache.getTotal_score();
		PlayerData playerData = playerDataMap.get(uid);
		PhzResGameBase phzResGameBase = new PhzResGameBase();
		phzResGameBase.getHands().addAll(playerData.getHands());//手牌数据
		for(PlayerData pData:playerDataMap.values()) {
			phzResGameBase.getOpdatamap().put(pData.getUid(), pData.getOp_datas());//废牌数据
			phzResGameBase.getBaipais().put(pData.getUid(), pData.getBaipais());
			if(pData.isQihu()) {
				phzResGameBase.getQihu_list().add(pData.getUid());
			}
			int score=scoremap.containsKey(pData.getUid())?scoremap.get(pData.getUid()):0;
			phzResGameBase.getScoremap().put(pData.getUid(), score);
		}
		OpData fanpai=phzGameBase.getFanpai();
		if(fanpai!=null&&fanpai.getBaipai_uid()==null) {
			phzResGameBase.setFanpai(fanpai);//翻牌
		}
		Map<Integer, List<CphOpData>> map=phzGameBase.getOplistmap();
		if(!map.isEmpty()) {
			Map<Integer,ReqOp> choosemap=phzGameBase.getChoosemap();
			if(choosemap!=null&&!choosemap.containsKey(uid)) {
				List<CphOpData> list=map.get(uid);
				if(list!=null) {
					phzResGameBase.getFanpaiops().addAll(list);//操作
				}
			}
		}
		phzResGameBase.setBanker_uid(phzGameBase.getBanker_uid());
		phzResGameBase.setDispatch_uid(phzGameBase.getDispatch_uid());
		phzResGameBase.setDipai_left(phzGameBase.getDipai().size());
		phzResGameBase.getBirds().putAll(phzRoomCache.getBirds());
		return phzResGameBase;
	}

	public static NotifyOutCard packageNotifyOutCard(int uid, Card card) {
		NotifyOutCard notifyOutCard = new NotifyOutCard();
		notifyOutCard.setUid(uid);
		notifyOutCard.setCard(card);
		return notifyOutCard;
	}

	public static NotifyCphOp packageNotifyCphOp(Room room,int cph_uid, List<CphOpData> list) {
		PhzConfig phzConfig = (PhzConfig) room.getGameConfig();
		NotifyCphOp notifyCphOp = new NotifyCphOp();
		notifyCphOp.setUid(cph_uid);
		notifyCphOp.getCphOpDatas().addAll(list);
		notifyCphOp.setLeft_timer(phzConfig.getCph_timer());
		return notifyCphOp;
	}

	public static ResOutCard packageResOutCard(int uid, Card card) {
		ResOutCard resOutCard = new ResOutCard();
		resOutCard.setUid(uid);
		resOutCard.setCard(card);
		return resOutCard;
	}

	public static ResSetcardsLeft packageResSetcardsLeft(Map<Integer, Integer> nummap) {
		ResSetcardsLeft resSetcardsLeft = new ResSetcardsLeft();
		resSetcardsLeft.getNummap().putAll(nummap);
		return resSetcardsLeft;
	}

	public static NotifyQihu packageNotifyQihu(int uid) {
		NotifyQihu notifyQihu = new NotifyQihu();
		notifyQihu.setUid(uid);
		return notifyQihu;
	}

	public static OperateData packageZhanjiBankerLastCard(int uid, Card card) {
		OperateData operateData = new OperateData();
		operateData.setType(ZhanjiType.BANKER_LAST_CARD);
		operateData.setCard(card);
		operateData.setUid(uid);
		return operateData;
	}

	public static OperateData packageZhanjiBaipai(int uid, List<TipaiData> tipaiDatas) {
		OperateData operateData = new OperateData();
		operateData.setType(ZhanjiType.BAIPAI);
		operateData.setUid(uid);
		operateData.getBaipais().addAll(tipaiDatas);
		return operateData;
	}

}
